/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.transfer;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.VehicleInfo;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wtkj.vo.dzzf.share.staticvalue.VehicleStaticValue;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.DataTransferDao;
import com.wttech.sjhj.check.moduleSet.CheckVehicleUpload_9;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.wttech.dzzf.process.constant.UserConstants.RIGHT;

/**
 * 数据迁移-卡片信息
 *
 * @author MaShibo
 * @date 2017/11/6.
 */
@Service
public class CardInfoCheckService {


    private static final Logger ERRORINFO = DataTransferService.ERRORINFO;

    /**
     * 建行默认网络编号
     */
    private static final int DEFAULT_CARDNETID = 6102;

    @Resource
    private DataTransferDao dataTransferDao;

    @Resource
    private VehicleInfoCheckService vehicleInfoCheckService;

    void analyseCardInfo(CardInfo cardInfo) {

        checkCardInfoIsValid(cardInfo);

        String cardInfoId = cardInfo.getCardInfoId();
        Integer idNum = dataTransferDao.queryCardInfo(cardInfoId);
        if (idNum != null && idNum >= 1) {
            ERRORINFO.info("卡片信息{}，ID【{}】已存在", cardInfoId, cardInfoId);
        }

        String vehPlate = cardInfo.getVehiclePlate();
        Integer vehColor = cardInfo.getVehicleColor();
        Integer cardNum = dataTransferDao.queryCardInfoByVehInfos(vehPlate, vehColor);
        if (cardNum != null && cardNum >= 1) {
            ERRORINFO.info("卡片信息{}，车牌号【{}】，车牌颜色【{}】对应卡片信息已存在", new Object[]{cardInfoId, vehPlate, vehColor});
        }

        VehicleInfo vehicleInfo = dataTransferDao.queryVehInfoByVehInfos(vehPlate, vehColor);
        if (vehicleInfo == null) {
            ERRORINFO.info("卡片信息{}，车牌号【{}】，车牌颜色【{}】未提供车辆信息记录", new Object[]{cardInfoId, vehPlate, vehColor});
        } else {
            vehicleInfoCheckService.analyseVehicleInfo(vehicleInfo);
        }

    }

    private void checkCardInfoIsValid(CardInfo cardInfo) {
        String cardInfoId = cardInfo.getCardInfoId();

        //卡片类型
        Integer ecardType = cardInfo.getECardType();
        boolean wrongType = ecardType == null || (!ecardType.equals(CardStaticValue.EcardType.CARD_TYPE_CZ) && !ecardType.equals(CardStaticValue.EcardType.CARD_TYPE_JZ));
        if (wrongType) {
            ERRORINFO.info("卡片信息{}，卡片类型【{}】错误", cardInfoId, ecardType);
        }

        //TOPCLASS
        String topClass = cardInfo.getTopClass();
        if (!StringUtils.equalsIgnoreCase(topClass, FundAccountCheckService.DEFAULT_TOPCLASS)) {
            ERRORINFO.info("卡片信息{}，TOPCLASS【{}】错误", cardInfoId, topClass);
        }

        //车辆信息
        String vehPlate = cardInfo.getVehiclePlate();
        Integer vehColor = cardInfo.getVehicleColor();
        String vehicleId = vehPlate + UserConstants.DEFAULT_VEHICLE_CHAR + vehColor;
        if (!StringUtils.equalsIgnoreCase(CheckVehicleUpload_9.checkId(vehicleId), RIGHT)) {
            ERRORINFO.info("卡片信息{}， 车辆ID【{}】错误", cardInfoId, vehicleId);
        }

        //车型
        Integer vehType = cardInfo.getVehicleType();
        if (!VehicleStaticValue.VehType.vehTypeMap().containsKey(vehType)) {
            ERRORINFO.info("卡片信息{}, 车辆类型【{}】错误", cardInfoId, vehType);
        }

        //网络编号
        Integer cardNetId = cardInfo.getCardNetID();
        if (cardNetId == null || cardNetId != DEFAULT_CARDNETID) {
            ERRORINFO.info("卡片信息{}， 网络编号【{}】错误", cardInfoId, cardNetId);
        }

    }
}
